
//
//  CourseSegmentedControl.m
//  ShowYork
//
//  Created by hxh on 2020/9/15.
//  Copyright © 2020 hxh. All rights reserved.
//
#define WIDTH kScreenW/self.titleArr.count
typedef void(^doneBlock)(NSInteger index);
#import "LessonSegmentedControl.h"

@interface LessonSegmentedControl ()
@property (nonatomic,copy)NSArray *titleArr;
@property (nonatomic,copy)doneBlock done;
@property (nonatomic,strong)UIView *lineView;
@property (nonatomic,strong)NSMutableArray *btnArray;
@end

@implementation LessonSegmentedControl

- (NSMutableArray *)btnArray{
    if (!_btnArray) {
        _btnArray = [NSMutableArray array];
    }
    return _btnArray;
}

- (instancetype)initWithSourceTitles:(NSArray<NSString *> *)titles completeBlock:(void (^)(NSInteger index))completeBlock{
    if (self = [super init]) {
        self.titleArr = titles;
        [self setUI];
        if (completeBlock) {
            self.done = ^(NSInteger index) {
                completeBlock(index);
            };
        }
    }
    return self;
}

- (void)setUI{
    self.frame = CGRectMake(0, 0, kScreenW, 44);
    self.backgroundColor = RGBA(248, 248, 248, 1);
    
    [self.titleArr enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = [[UIButton alloc]init];
        [btn setTitle:obj forState:UIControlStateNormal];
        [btn setTitleColor:RGBA(51, 51, 51, 1) forState:UIControlStateNormal];
        btn.titleLabel.font = font(16);
        btn.tag = idx;
        [btn addTarget:self action:@selector(coverMethod:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self).offset(idx * WIDTH);
            make.top.bottom.mas_equalTo(self);
            make.width.mas_equalTo(WIDTH);
        }];
        
        [self.btnArray addObject:btn];
    }];
    
    self.lineView = [[UIView alloc]init];
    self.lineView.layer.cornerRadius = 2.5;
    self.lineView.layer.masksToBounds = YES;
    self.lineView.backgroundColor = [RPAppManager themColor];
    [self addSubview:self.lineView];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(26, 4));
        make.left.mas_equalTo(self).offset(WIDTH/2 - 13);
        make.bottom.mas_equalTo(self);
    }];
    
}

- (void)coverMethod:(UIButton *)btn{
    [self layoutIfNeeded];
    
    
    [self.lineView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(CGRectGetMinX(btn.frame) + WIDTH/2 - 13);
    }];
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    }];
    
    if (self.done) {
        self.done(btn.tag);
    }
    
}

- (void)setCurrentIndex:(NSInteger)currentIndex{
    _currentIndex = currentIndex;
    
    UIButton *btn =  self.btnArray[currentIndex];
    
    [self layoutIfNeeded];
    
    [self.lineView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(CGRectGetMinX(btn.frame) + WIDTH/2 - 13);
    }];
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    }];
}

- (void)showInView:(UIView *)view{
    [view addSubview:self];
}
@end
